using System.Collections;
using Holoville.HOTween;
using UnityEngine;

public class OfflineInternetChecker : PhotonMonoMessagesHandler
{
	public UISprite indicator;

	public float checkRate = 2f;

	private IEnumerator checker;

	private Tweener glowTween;

	[HideInInspector]
	public float _alpha;

	private void Awake()
	{
		GetComponent<OfflineInternetChecker>().enabled = false;
		base.gameObject.SetActive(false);
	}

	private void Start()
	{
		ChangeGameMode();
	}

	private void ChangeGameMode()
	{
		if (settings.offlineMode)
		{
			base.gameObject.SetActive(true);
			if (indicator != null)
			{
				indicator.alpha = 0f;
			}
			StartCoroutine(ConnectWithDelay());
		}
		else
		{
			checker = null;
			base.gameObject.SetActive(false);
		}
	}

	private IEnumerator ConnectWithDelay()
	{
		yield return new WaitForSeconds(4f);
		PhotonNetwork.ConnectUsingSettings(settings.verConnectPhoton);
	}

	private IEnumerator CheckInternetAvailability()
	{
		if (indicator != null)
		{
			indicator.color = Color.red;
			while (true)
			{
				indicator.alpha = ((!PhotonNetwork.connected) ? 1f : 0f);
				yield return new WaitForSeconds(checkRate);
			}
		}
		yield return null;
	}

	private void Setindicator()
	{
		if (checker == null)
		{
			checker = CheckInternetAvailability();
			StartCoroutine(checker);
			HOTween.Kill(glowTween);
		}
	}

	public void OnConnectedToPhoton()
	{
		Setindicator();
	}

	public void OnFailedToConnectToPhoton(object parameters)
	{
		Setindicator();
	}

	public void OnDisconnectedFromPhoton()
	{
		StartCoroutine(ConnectWithDelay());
	}

	private void OnDestroy()
	{
	}
}
